package com.dlut.preparedStatement.crud;

import com.dlut.connection.ConnectionTest;
import com.dlut3.util.JDBCUtil;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * 使用PreparedStatement实现对数据表的增删改操作
 * 增删改， 查
 *
 */
public class PreparedStatementUpdateTest {

    @Test
    public void test3(){
//        String sql = "delete from customers where id = ?";
//        update(sql,3);

        String sql = "update `order` set order_name = ? where order_id = ?";
        update(sql, "DD",2);
    }

    //通用的增删改操作
    public void update(String sql, Object ...args){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //1.获取数据库的链接
            conn = JDBCUtil.getConnection();
            //2.预编译sql语句
            ps = conn.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1, args[i]);
            }
            //4.执行操作
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5.资源的关闭
            JDBCUtil.closeResource(conn,ps);
        }
    }

    //修改记录
    @Test
    public void test2() {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //1. 获取数据库的链接
            conn = JDBCUtil.getConnection();
            //2.预编译sql语句
            String sql = "update customers set name = ? where id = ?";
            ps = conn.prepareStatement(sql);
            //3.填充占位符
            ps.setObject(1,"莫扎特");
            ps.setObject(2,18);
            //4.执行操作
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5.资源的关闭
            JDBCUtil.closeResource(conn,ps);
        }

    }

    // 增加一条记录
    @Test
    public void test1(){
        PreparedStatement ps = null;
        Connection conn = null;
        try {
            //1. 读取配置文件中的4个基本信息
            InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");

            Properties pros = new Properties();
            pros.load(is);

            String user = pros.getProperty("user");
            String password = pros.getProperty("password");
            String url = pros.getProperty("url");
            String driverClass = pros.getProperty("driverClass");

            //2. 加载驱动
            Class.forName(driverClass);

            //3. 获取链接
            conn = DriverManager.getConnection(url, user, password);

            //4.预编译sql语句，返回PreparedStatement的实例
            String sql = "insert into customers(name,email,birth)values(?,?,?)";
            ps = conn.prepareStatement(sql);

            //5. 填充占位符
            ps.setString(1,"哪吒");
            ps.setString(2,"nezha@mail.com");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse("1000-01-01");
            ps.setDate(3,  new java.sql.Date(date.getTime()));

            //6. 执行操作
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            //7.资源关闭
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }



    }
}
